// stdafx.cpp : source file that includes just the standard includes
// HaDoPhucTapThuatToan.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"
void NhapMang (int *a,int &n)
{
	printf("Nhap so luong phan tu cua mang: ");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		printf("a[%d]= ",i);
		scanf("%d",&a[i]);
	}
}

/*int SMax (int *a,int n,int &start,int &end)
{
start=end=-1;
int Max=0;
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
int sum=0;

for(int k=i;k<=j;k++)
{
sum+=a[k];
}
if(sum>Max)
{
Max=sum;
start=i;
end=j;
}
}
}
return (Max<0?0:Max);
}
*/
/*int SMax (int *a,int n,int &start,int &end)
{
int Max=0;
start=end=-1;
int sum=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
sum+=a[j];
if(sum>Max)
{
Max=sum;
start+=i;
end=j;
}
}
}
return (Max<0?0:Max);
}*/
int SMax (int *a,int n,int &start,int &end)
{
	int sum=0,max=0,dem=0;
	start=end=-1;
	for(int i=0;i<n;i++)
	{
		sum+=a[i];

		if(sum>max)
		{
			max=sum;
			if(start==-1||end==-1)
			{
				start=end=i;
			}
			else
			{
				end=i;
				if(dem!=0)
					start=i-dem;
			}
		}
		else
		{
			if(sum+a[i+1]>sum&&sum>=0)
			{
				dem++;
			}
			else
			{
				sum=0;
				dem=0;
			}
		}
	}
	return (max<0?0:max);
}
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
